<?php
$loader->requireOnce('ordo/PayPayment.class.php');

class TreatmentDuration {
	
	/**
	 * Add new treatment duration for a patient
	 *
	 * This function does not care whether there is any
	 * opening treatment duration available or not
	 *
	 * @param $patientId
	 * @param $departmentId
	 * @param $enumPatientType	In/Out patient - enum type
	 * @return
	 * 		ticketId
	 */
	public function createTreatmentDuration($patientId, $departmentId, $enumPatientType) {
		$ticketObj = Celini::newORDO('Ticket');
		$ticketObj->addNewTicket($patientId, $departmentId, $enumPatientType);
		return $ticketObj->get_id();
	}
	
	/**
	 * Check if patient have paid for all services.
	 *
	 * @param int $ticketId
	 * @return boolean
	 */
	public function isFullyPaid($ticketId) {
	    $payment = PayPayment::getObjectByTicket($ticketId);
	    return $payment->get('patient_price') > $payment->get('real_payment');
	}
	
	/**
	 * Close an treatment duration.
	 *
	 * @param $ticketId
	 * @return Void
	 */
	public function closeTreatmentDuration($ticketId) {
		Celini::newORDO('Ticket')->close($ticketId);
		Celini::newORDO('MedicalRecord')->closeTicket($ticketId);
	}
	
	/**
	 * get lasted opening treatment duration of given patient id
	 *
	 * @param $patientId
	 * @param $departmentId
	 * @return
	 * 		ticketId	if an opening treatment duration available
	 * 		false		if there is no opening treatment duration available
	 */
	public function getLastedOpeningTreatmentDuration($patientId, $departmentId) {
		return Celini::newORDO('Ticket')->isAnyOpenTicketByDepartment($patientId, $departmentId);
	}
	
	/**
	 * The treatment duration of $ticketId must be OPEN
	 * If there is no treatment instance for today, create new instance
	 *
	 * @param $ticketId		The ticket status must be 'open'
	 * @return rhmTreatmentInstanceId
	 */
	public function setupTreatmentInstance($ticketId, $patientId) {
		$ins = Celini::newORDO('RhmTreatmentInstance');
		$treatmentId = $ins->isTreatmentInstanceAvailableForToday($ticketId);
		// Available record for today
		if ($treatmentId !== false) {
			return $treatmentId;
		}
		
		// Create new record
		return $ins->createTreatmentInstance($ticketId, $patientId);
	}
}
?>